//
//  CYTextView.m
//  LingLingBang
//
//  Created by 郝俊聪 on 15/12/28.
//  Copyright © 2015年 zhaohongling. All rights reserved.
//

#import "CYTextView.h"
#import "CYEmoticonCell.h"
#import "CYEmoticon.h"
#import "CYTextAttachment.h"

@implementation CYTextView

///插入表情的方法
- (void)insertEmoticon:(CYEmoticonCell *)cell
{
    //取出数据模型
    CYEmoticon *emoticon = cell.emoticon;
    //执行插入表情方法
    [self textViewInsertEmoticon:emoticon];
}

///插入表情的具体操作
- (void)textViewInsertEmoticon:(CYEmoticon *)emoticon
{
    //如果是空按钮
    if (emoticon.isEmpty) {
        DLog(@"空");
        return;
    }
    //如果是删除按钮
    if (emoticon.isDelete) {
        //删除
        [self deleteText];
        return;
    }
    //如果是表情
    if (emoticon.png) {
        //插入表情
        [self insertImage:emoticon];
        return;
    }
}

///删除文本
- (void)deleteText
{
    [self deleteBackward];
}

///插入图片
- (void)insertImage:(CYEmoticon *)emoticon
{
    //设置字体
    UIFont *font = SYSTEMFONT(15);
    if (self.currentfont) {
        font = self.currentfont;
    }
    //创建自定义附件
    CYTextAttachment *textAtta = [[CYTextAttachment alloc] init];
    //设置照片
    textAtta.image = [UIImage imageNamed:emoticon.png];
    //插入同时，在自定义附件中 记录str
    textAtta.str = emoticon.str;
    //设置附件的大小
    textAtta.bounds = CGRectMake(0, -4,font.lineHeight,font.lineHeight);
    //将附件转成NSAttributedString
    NSAttributedString *attri = [NSAttributedString attributedStringWithAttachment:textAtta];
    //建text的文本转化为可变属性文本
    NSMutableAttributedString *attStrM = [[NSMutableAttributedString alloc] initWithAttributedString:self.attributedText];
    //获得目前光标的位置
    NSRange selectedRange = self.selectedRange;
    //插入文字
    [attStrM insertAttributedString:attri atIndex:selectedRange.location];
    //设置可变文本的属性
    [attStrM addAttribute:NSFontAttributeName value:font range:NSMakeRange(0, attStrM.length)];
    //再次记住光标位置
    NSRange newSelectedRange = NSMakeRange(selectedRange.location+1, 0);
    //重新给文本赋值
    self.attributedText = attStrM;
    //恢复光标的位置（上面一句代码执行之后，光标会移到最后面）
    self.selectedRange = newSelectedRange;
}

///将textView中的图片转成字符
- (NSString *)fullText
{   //获得属性文本
    NSAttributedString *attriText = self.attributedText;
    //定义一个字符串用于接收转换的字符
    __block NSString *strM = @"";
    //遍历属性文本
    [attriText enumerateAttributesInRange:NSMakeRange(0, attriText.length) options:NSAttributedStringEnumerationLongestEffectiveRangeNotRequired  usingBlock:^(NSDictionary<NSString *,id> * _Nonnull attrs, NSRange range, BOOL * _Nonnull stop) {
        //取得附件
        CYTextAttachment *attachment = attrs[@"NSAttachment"];
        if (attachment) {
            //是表情
            strM = [strM stringByAppendingString:attachment.str];
        }else{
            //是文字
            NSString *str = [attriText.string substringWithRange:range];
            //拼接
            strM = [strM stringByAppendingString:str];
        }
    }];
    return strM;
}

+ (NSString *)fullTextWithAttriStr:(NSAttributedString *)attriStr
{
    //定义一个字符串用于接收转换的字符
    __block NSString *strM = @"";
    //遍历属性文本
    [attriStr enumerateAttributesInRange:NSMakeRange(0, attriStr.length) options:NSAttributedStringEnumerationLongestEffectiveRangeNotRequired  usingBlock:^(NSDictionary<NSString *,id> * _Nonnull attrs, NSRange range, BOOL * _Nonnull stop) {
        //取得附件
        CYTextAttachment *attachment = attrs[@"NSAttachment"];
        if (attachment) {
            //是表情
            strM = [strM stringByAppendingString:attachment.str];
        }else{
            //是文字
            NSString *str = [attriStr.string substringWithRange:range];
            //拼接
            strM = [strM stringByAppendingString:str];
        }
    }];
    return strM;
}

//粘贴的时候
- (void)paste:(id)sender
{
    UIPasteboard *board = [UIPasteboard generalPasteboard];
    NSMutableAttributedString *attriStrM = [[NSMutableAttributedString alloc] initWithAttributedString:self.attributedText];
    //将粘贴文字转化为属性文本
    NSAttributedString *emoticonStr = [CYEmoticon emoticonStringWith:board.string lineHeight:10];
//    [attriStrM appendAttributedString:emoticonStr];
    NSRange selectedRange = self.selectedRange;
    NSUInteger index = selectedRange.location;
    [attriStrM insertAttributedString:emoticonStr atIndex:index];
    //设置字体
    [attriStrM addAttribute:NSFontAttributeName value:self.font range:NSMakeRange(0, attriStrM.length)];
    self.attributedText = attriStrM;
    if (self.attributedText.length > 0 || self.textViewPaste) {
        self.textViewPaste();
    }
    //设置新光标位置
    self.selectedRange =  NSMakeRange(selectedRange.location+emoticonStr.length, 0);
//    [self resignFirstResponder];
}

//复制的时候
-(void)copy:(id)sender

{
    UIPasteboard *pboard = [UIPasteboard generalPasteboard];
    //将属性文本转为普通文本
    NSRange selectedRange = self.selectedRange;
    NSAttributedString *mStr = [self.attributedText attributedSubstringFromRange:selectedRange];
    NSString *string = [CYTextView fullTextWithAttriStr:mStr];
    pboard.string = string;
}

@end
